const state = {
  token: localStorage.getItem("token")?localStorage.getItem("token"):" ",
  userInfo: localStorage.getItem("userInfo")?localStorage.getItem("userInfo"):" ",
};

const mutations = {
  SET_TOKEN: (state, token) => {
    localStorage.setItem("token", token);
    state.token = token;
  },
  REMOVE_TOKEN(){
    state.token=''
    localStorage.removeItem("token")
  },
  SET_USERINFO: (state, userInfo) => {
    localStorage.setItem("userInfo", userInfo);
    state.userInfo=userInfo
  },
  REMOVE_USERINFO(){
    state.userInfo=" "
    localStorage.removeItem("userInfo")
  }
};

const actions = {
  SET_TOKEN({commit},token){
    commit("SET_TOKEN",{token})
  },
  REMOVE_TOKEN({commit}){
    commit("REMOVE_TOKEN")
  },
  SET_USERINFO({commit},userInfo){
    commit("SET_USERINFO",{userInfo})
  },
  REMOVE_USERINFO({commit}){
    commit("REMOVE_USERINFO")
  },
  //登录
  login({commit},data){
    // console.log("action:", data.token,data.userInfo);
    commit("SET_TOKEN",data.token);
    commit("SET_USERINFO", data.userInfo);
  },
  //退出登录
  loginout({commit}){
    commit("REMOVE_TOKEN")
    commit("REMOVE_USERINFO")
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
